package com.sbr.tbd.test.dao.jpa;

import java.sql.Connection;

import javax.sql.DataSource;

import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/applicationContextTest.xml"})
@Transactional
public class AbstractJpaDao {
	@Autowired
	private DataSource dataSource;

	@Before
	public void setUp() throws Exception {
		//get connection
		Connection con = dataSource.getConnection();
		IDatabaseConnection dbUnitCon = new DatabaseConnection(con);
		//get dataset
	
		FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
		
		IDataSet dataSet =  builder.build(getClass().getResource("/userdataset.xml"));
		
		try {
           DatabaseOperation.CLEAN_INSERT.execute(dbUnitCon, dataSet);
        } finally {
            DataSourceUtils.releaseConnection(con, dataSource);
        }
	}

	@After
	public void tearDown() throws Exception {
	}

}
